package behaviorPattern.observer;

/**
 * @Author: Alex Hu
 * Date: 2020/2/22 1:50 下午
 * Content:
 */

import java.util.Observable;
import java.util.Observer;

public class CrudeOilFutures {
    public static void main(String[] args) {
        OilFutures oil = new OilFutures();
        /**多方*/
        Observer bull = new Bull();
        /**空方*/
        Observer bear = new Bear();
        oil.addObserver(bull);
        oil.addObserver(bear);
        oil.setPrice(10);
        oil.setPrice(-8);
    }
}

/**
 * 具体目标类：原油期货
 */
class OilFutures extends Observable {
    private float price;

    public float getPrice() {
        return this.price;
    }

    public void setPrice(float price) {
        /**设置内部标志位，注明数据发生变化*/
        super.setChanged();
        /**通知观察者价格改变了*/
        super.notifyObservers(price);
        this.price = price;
    }
}

/**
 * 具体观察者类：多方
 */
class Bull implements Observer {
    @Override
    public void update(Observable o, Object arg) {
        Float price = ((Float) arg).floatValue();
        if (price > 0) {
            System.out.println("油价上涨" + price + "元，多方高兴了！");
        } else {
            System.out.println("油价下跌" + (-price) + "元，多方伤心了！");
        }
    }
}

/**
 * 具体观察者类：空方
 */
class Bear implements Observer {
    @Override
    public void update(Observable o, Object arg) {
        Float price = ((Float) arg).floatValue();
        if (price > 0) {
            System.out.println("油价上涨" + price + "元，空方伤心了！");
        } else {
            System.out.println("油价下跌" + (-price) + "元，空方高兴了！");
        }
    }
}
